package com.cw.lang.redis.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ScanOptions;

import com.cw.lang.redis.service.RedisForHashService;

import lombok.Getter;
import lombok.Setter;

/**
 * redisTemplate.opsForHash()操作hash
 *
 * @author chenw
 * @version $$Id: RedisForHashServiceImpl.java, v 0.1 2018/11/19 3:34 PM chenw Exp $$
 */
@Getter
@Setter
public class RedisForHashServiceImpl<H, HK, HV> extends AbstractRedisService<H, HV>
    implements RedisForHashService<H, HK, HV> {

    /**
     * {@link HashOperations}，主要为了解决泛型问题
     *
     * @param key key
     * @return {@link HashOperations}
     */
    private HashOperations<H, HK, HV> getHashOperations(H key) {
        return getRedisTemplate(key).opsForHash();
    }

    @Override
    public Long delete(H key, Object... hashKeys) {
        return getHashOperations(key).delete(key, hashKeys);
    }

    @Override
    public Boolean hasKey(H key, Object hashKey) {
        return getHashOperations(key).hasKey(key, hashKey);
    }

    @Override
    public HV get(H key, Object hashKey) {
        return getHashOperations(key).get(key, hashKey);
    }

    @Override
    public List<HV> multiGet(H key, Collection<HK> hashKeys) {
        return getHashOperations(key).multiGet(key, hashKeys);
    }

    @Override
    public Long increment(H key, HK hashKey, long delta) {
        return getHashOperations(key).increment(key, hashKey, delta);
    }

    @Override
    public Double increment(H key, HK hashKey, double delta) {
        return getHashOperations(key).increment(key, hashKey, delta);
    }

    @Override
    public Set<HK> keys(H key) {
        return getHashOperations(key).keys(key);
    }

    @Override
    public Long size(H key) {
        return getHashOperations(key).size(key);
    }

    @Override
    public void putAll(H key, Map<? extends HK, ? extends HV> m) {
        getHashOperations(key).putAll(key, m);
    }

    @Override
    public void put(H key, HK hashKey, HV value) {
        getHashOperations(key).put(key, hashKey, value);
    }

    @Override
    public Boolean putIfAbsent(H key, HK hashKey, HV value) {
        return getHashOperations(key).putIfAbsent(key, hashKey, value);
    }

    @Override
    public List<HV> values(H key) {
        return getHashOperations(key).values(key);
    }

    @Override
    public Map<HK, HV> entries(H key) {
        return getHashOperations(key).entries(key);
    }

    @Override
    public Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options) {
        return getHashOperations(key).scan(key, options);
    }

    @Override
    public RedisOperations<H, ?> getOperations() {
        return getRedisTemplate().opsForHash()
            .getOperations();
    }
}
